﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DocFieldCls
    {
        public string DocFieldId;
        public string DocFieldName;
        public int    SortIndex=0;
        public string frkGroupDataId;
        public int TotalItem = 0;
    }

    public class DocFieldParser
    {
        public static DocFieldCls CreateInstance()
        {
            //tao 1 class trang....
            DocFieldCls oDocFieldCls = new DocFieldCls();
            return oDocFieldCls;
        }

        public static DocFieldCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DocFieldCls oDocFieldCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDocFieldCls;
        }


        public static DocFieldCls ParseFromDataRow(DataRow dr)
        {
            DocFieldCls oDocFieldCls = new DocFieldCls();
            oDocFieldCls.DocFieldId = XmlUtility.GetString(dr, "DocFieldId", true);
            oDocFieldCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oDocFieldCls.DocFieldName = XmlUtility.GetString(dr, "DocFieldName", true);
            oDocFieldCls.SortIndex = XmlUtility.GetInt(dr, "SortIndex", true);
            oDocFieldCls.TotalItem = XmlUtility.GetInt(dr, "TotalItem", true);
            return oDocFieldCls;
        }

        public static DocFieldCls[] ParseFromDataTable(DataTable dtTable)
        {
            DocFieldCls[] DocFields = new DocFieldCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DocFields[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DocFields;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("DocFieldId");
            ds.Tables["Info"].Columns.Add("frkGroupDataId");
            ds.Tables["Info"].Columns.Add("DocFieldName");
            ds.Tables["Info"].Columns.Add("SortIndex");
            ds.Tables["Info"].Columns.Add("TotalItem");
            return ds;
        }

        public static string GetXml(DocFieldCls oDocFieldCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oDocFieldCls.DocFieldId,
                oDocFieldCls.frkGroupDataId,
                oDocFieldCls.DocFieldName,
                oDocFieldCls.SortIndex,
                oDocFieldCls.TotalItem,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            DocFieldCls oDocField = DocFieldParser.ParseFromDataRow(dr);
            string Xml = DocFieldParser.GetXml(oDocField);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            DocFieldCls[] DocFields = new DocFieldCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                DocFields[iIndex] = DocFieldParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = DocFieldParser.GetXml(DocFields);
            return Xml;
        }


        public static string GetXml(DocFieldCls[] DocFields)
        {
            if (DocFields == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                DocFields = new DocFieldCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < DocFields.Length; iIndex++)
            {
                DocFieldCls oDocFieldCls = DocFields[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oDocFieldCls.DocFieldId,
                    oDocFieldCls.frkGroupDataId,
                    oDocFieldCls.DocFieldName,
                    oDocFieldCls.SortIndex,
                    oDocFieldCls.TotalItem,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static DocFieldCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DocFieldCls[0];
            }
            DocFieldCls[] DocFields = new DocFieldCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DocFields[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DocFields;
        }
    }
}
